﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FRW.Common.Extensions
{
    public static class IOUtils
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <param name="oldPath"></param>
        /// <param name="newPath"></param>
        /// <param name="newPathOffset"></param>
        /// <returns></returns>
        public static FileInfo NewPath(this FileInfo fileInfo, string oldPath, string newPath, string newPathOffset = @"\")
        {
            string newUpdatedPath = newPath + @"\" + newPathOffset + @"\";

            if (fileInfo != null)
            {
                DirectoryInfo sourcePath = new DirectoryInfo(oldPath);
                DirectoryInfo destinationPath = new DirectoryInfo(newUpdatedPath);

                newUpdatedPath = UpdatePath(fileInfo.FullName, sourcePath.FullName, destinationPath.FullName);
                fileInfo = new FileInfo(newUpdatedPath);
            }

            return fileInfo;
        }

        public static string Path(this FileInfo fileInfo)
        {
            try
            {
                return fileInfo.FullName.Replace(fileInfo.Name, "");
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="directoryInfo"></param>
        /// <param name="oldPath"></param>
        /// <param name="newPath"></param>
        /// <param name="newPathOffset"></param>
        /// <returns></returns>
        public static DirectoryInfo NewPath(this DirectoryInfo directoryInfo, string oldPath, string newPath, string newPathOffset = @"\")
        {
            string newUpdatedPath = newPath + @"\" + newPathOffset + @"\";

            if (directoryInfo != null)
            {
                DirectoryInfo sourcePath = new DirectoryInfo(oldPath);
                DirectoryInfo destinationPath = new DirectoryInfo(newUpdatedPath);

                newUpdatedPath = UpdatePath(directoryInfo.FullName, sourcePath.FullName, destinationPath.FullName);
                directoryInfo = new DirectoryInfo(newUpdatedPath);
            }

            return directoryInfo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        private static string UpdatePath(string path, string source, string destination)
        {
            String[] filePath = path.Split('\\');
            String[] sourcePath = source.Split('\\');

            int i = 0;

            while (i < filePath.Count() && i < sourcePath.Count())
            {
                if (filePath[i].ToLower().Equals(sourcePath[i].ToLower()))
                {
                    i++;
                }
                else
                {
                    break;
                }
            }

            if (i == sourcePath.Count())
            {
                while (i < filePath.Count())
                {
                    destination += @"\" + filePath[i++];
                }
            }
            else
            {
                destination = path;
            }

            return destination;
        }
    }
}